package com.example.leetcode.trainingcamp.week12;

public class Offer21 {

    public int[] exchange(int[] nums) {
        int start = 0;
        int end = nums.length -1;
        while (start <end){
            int startValue = nums[start];
            int endValue = nums[end];
            if (startValue % 2 != 0){
                start++;
            }else if (endValue % 2 !=1){
                end--;
            }else {
                swap(nums,start,end);
            }
        }
        return nums;
    }

    private void swap(int[] nums,int source,int target) {
        int t = nums[source];
        nums[source] = nums[target];
        nums[target] = t;
    }

    public static void main(String[] args) {
        Offer21 o = new Offer21();
        int[] nums = {1,2,3,4};
        int[] exchange = o.exchange(nums);
        for (int i:exchange){
            System.out.println(i + ",");
        }
    }
}
